/***************************************************************************
 *  WlanSavvy
 *  Copyright  2007  Deepak Manoharan
 *  Email deepakm@gmail.com
 *  http://wlansavvy.googlecode.com/
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "panellist.h"

static GtkTreeModel *
create_and_fill_model (void)
{
	GtkListStore  *store;
	GtkTreeIter    iter;
	GdkPixbuf *pixbuf;
    GtkWidget *cellview;

	cellview = gtk_cell_view_new ();  
	store = gtk_list_store_new (NUM_COLS, GDK_TYPE_PIXBUF, G_TYPE_STRING);
    
	/* Append Connection details row */
	pixbuf = gtk_widget_render_icon (cellview, GTK_STOCK_CONNECT,
                                         GTK_ICON_SIZE_DND, NULL);
	
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter,
                      COL_ICON, pixbuf,
                      COL_TEXT, "Connection",
                      -1);
  
	/* append scan row */
	pixbuf = gtk_widget_render_icon (cellview, GTK_STOCK_NETWORK,
                                         GTK_ICON_SIZE_DND, NULL);
	
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter,
                      COL_ICON, pixbuf,
                      COL_TEXT, "Scan",
                      -1);
  
	/* Append events row */
	pixbuf = gtk_widget_render_icon (cellview, GTK_STOCK_EXECUTE,
                                         GTK_ICON_SIZE_DND, NULL);
	
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter,
                      COL_ICON, pixbuf,
                      COL_TEXT, "Events",
                      -1);
    
	gtk_widget_destroy (cellview);
	
	return GTK_TREE_MODEL (store);
}

void 
change_displayed_panel(gint panel_val)
{
	GtkWidget * panel_connection = glade_xml_get_widget (xml, "panel_connection");
	GtkWidget * panel_scan = glade_xml_get_widget (xml, "panel_scan");
	GtkWidget * panel_event = glade_xml_get_widget (xml, "panel_event");
	
	gtk_widget_hide(panel_connection);
	gtk_widget_hide(panel_scan);
	gtk_widget_hide(panel_event);
	
	switch(panel_val)
	{
		case 0:
			gtk_widget_show(panel_connection);
			break;
		case 1:
			gtk_widget_show(panel_scan);
			break;
		case 2:
			gtk_widget_show(panel_event);
			break;
		default:
			gtk_widget_show(panel_connection);		
	}
	
}

void
panel_selection_changed(GtkTreeSelection *treeselection,
                        gpointer user_data)
{
	GtkTreeView * panel_list;
	GtkTreeModel *model;
	GtkTreeIter iter;
	GtkTreePath *path;
	
	panel_list = gtk_tree_selection_get_tree_view(treeselection);
	model = gtk_tree_view_get_model(panel_list);
	if (gtk_tree_selection_get_selected(treeselection, &model, &iter))
	{
		path = gtk_tree_model_get_path(model,&iter);
		change_displayed_panel(*(gtk_tree_path_get_indices(path)));
		gtk_tree_path_free(path);
	}
}

void 
init_panel_list()
{
	GtkCellRenderer *renderer;
	GtkTreeModel *model;
	GtkWidget * panel_list = glade_xml_get_widget (xml, "panel_list");
	GtkTreeSelection*  panel_selection = 
					gtk_tree_view_get_selection(GTK_TREE_VIEW(panel_list));
	GtkTreePath * initial_sel_path = gtk_tree_path_new_first();
	
	/* Column 1 */
	renderer = gtk_cell_renderer_pixbuf_new();
	gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(panel_list),
				-1,"Icon",renderer,"pixbuf",COL_ICON,NULL);
	
	/* Column 2 */
	renderer = gtk_cell_renderer_text_new();
	gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(panel_list),
				-1,"Text",renderer,"text",COL_TEXT,NULL);
	
	model = create_and_fill_model();

	/*Listen for panel selection changes*/	
	g_signal_connect(panel_selection, "changed", 
					(GCallback)panel_selection_changed, NULL);
	
	gtk_tree_view_set_model (GTK_TREE_VIEW (panel_list), model);
	
	//Set intial selection
	gtk_tree_selection_select_path(panel_selection,initial_sel_path);
	
	gtk_tree_path_free(initial_sel_path);
	g_object_unref (model);
	
	/*Initialize all panels*/
	init_connection_panel();
}
